package com.myblog.controller;


import cn.hutool.crypto.SecureUtil;
import com.myblog.common.lang.Result;
import com.myblog.entity.User;
import com.myblog.service.UserService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author From:星羽恒
 * @since 2021-11-24
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/register")
    public Result userRegister(@RequestBody User user){

        //将获得的密码进行处理, (解密时，只需要把再加密一次，然后比对即可！)
        String salt = new SecureRandomNumberGenerator().nextBytes().toString();
        String pwdPlus = new SimpleHash("md5",user.getPassword(),salt,3).toString();
        user.setPassword(pwdPlus);
        user.setSalt(salt);
        if(user.getAvatar().length()==0){
            user.setAvatar(null);
        }
        userService.saveOrUpdate(user);
        user.toProtected();
        return Result.succ(user);
    }
}
